package com.oa.entities;

import com.oa.domain.UndeleteEntity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@Entity
@Table(name = "oa_department")
public class Department extends UndeleteEntity<Long> {
    /**
     *
     */
    private static final long serialVersionUID = -5864349523681237840L;

    /** 部门的父级id **/
    public static final String DEPARTMENT_PARENT_ID="parentId";

    public static final String DEPARTMENT_NAME="name";

    /** 部门的名称 **/
    private String name;

    /** 部门的说明 **/
    private String depDesc;

    /** 部门的层次 **/
    private Integer depLevel;

    /** 路径 **/
    private String path;

    /** 部门的上级 **/
    private Department parent;

    private Long parentId;

    @Override
    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return super.getId();
    }

    @Override
    public void setId(Long id) {
        super.setId(id);
    }

    @NotNull
    @Column(name = "department_name",nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NotNull
    @Column(name = "department_desc",nullable = false)
    public String getDepDesc() {
        return depDesc;
    }

    public void setDepDesc(String depDesc) {
        this.depDesc = depDesc;
    }

    @Column(name = "department_level", nullable = false, columnDefinition = "int default 0")
    public Integer getDepLevel() {
        return depLevel;
    }

    public void setDepLevel(Integer depLevel) {
        this.depLevel = depLevel;
    }

    @Column(name = "department_path")
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id",insertable = false,updatable = false)
    public Department getParent() {
        return parent;
    }

    public void setParent(Department parent) {
        this.parent = parent;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Department that = (Department) o;
        return Objects.equals(name, that.name) &&
                Objects.equals(depDesc, that.depDesc) &&
                Objects.equals(depLevel, that.depLevel) &&
                Objects.equals(path, that.path) &&
                Objects.equals(parent, that.parent);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, depDesc, depLevel, path, parent);
    }

    @Override
    public String toString() {
        return "Department{" +
                "name='" + name + '\'' +
                ", depDesc='" + depDesc + '\'' +
                ", depLevel=" + depLevel +
                ", path='" + path + '\'' +
                ", parent=" + parent +
                '}';
    }

    @Column(name = "parent_id")
    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }
}
